package gcb.metalsys.controller.cop;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import gcb.metalsys.entity.auth.AuthUser;
import gcb.metalsys.service.auth.AuthUserService;
import gcb.metalsys.service.cop.CopWarehouseService;
import org.springframework.security.core.Authentication;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import gcb.metalsys.base.ResultUtil;
import gcb.metalsys.base.dto.ResultDTO;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import gcb.metalsys.service.cop.ProTransferService;
import gcb.metalsys.entity.cop.ProTransfer;

/**
 * @since 2020-07-15
 */
@RestController
@RequestMapping("/proTransfer")
public class ProTransferController {

    @Autowired
    private ProTransferService proTransferService;
    @Autowired
    private AuthUserService authUserService;
    @Autowired
    private CopWarehouseService copWarehouseService;

    @PostMapping
    @ApiOperation(value = "插入")
    public ResultDTO insertOne(@RequestBody ProTransfer proTransfer) throws Exception {
        proTransferService.save(proTransfer);
        return ResultUtil.Success(proTransfer);
    }

    @PutMapping
    @ApiOperation(value = "按ID修改")
    public ResultDTO updateById(@RequestBody ProTransfer proTransfer) throws Exception {
        proTransferService.updateById(proTransfer);
        return ResultUtil.Success();
    }

    @DeleteMapping("/{id:\\d+}")
    @ApiOperation("按ID删除")
    public ResultDTO deleteById(@PathVariable Long id) throws Exception {
        proTransferService.removeById((long) id);
        return ResultUtil.Success();
    }

    @GetMapping()
    @ApiOperation("查询、分页返回")
    public ResultDTO selectEntityPage(@RequestBody ProTransfer proTransfer,
                                            @RequestParam(value = "page", defaultValue = "0") int pageNum,
                                            @RequestParam(value = "limit", defaultValue = "8") int pageSize)throws Exception{
        QueryWrapper<ProTransfer> proTransferQueryWrapper = new QueryWrapper<>();
        //proTransferQueryWrapper.eq("name",test.getName());
        IPage<ProTransfer> page = proTransferService.page(new Page<ProTransfer>(pageNum, pageSize), proTransferQueryWrapper);
        List<ProTransfer> data = page.getRecords();
        int count = (int) page.getTotal();
        return ResultUtil.Success(data,count);
    }

    @GetMapping("/{id:\\d+}")
    @ApiOperation("ID查询")
    public ResultDTO selectEntityPage(@PathVariable Long id)throws Exception{
        return ResultUtil.Success(proTransferService.getById(id));
    }

    @PostMapping("/transfer")
    @Transactional
    public ResultDTO transfer(Authentication authentication,
                              @RequestBody ProTransfer proTransfer)throws Exception{
        AuthUser outUser = authUserService.getUserDetails(authentication);
        proTransfer.setOutUserId(outUser.getUserId());
        proTransfer.setTransferTime(LocalDateTime.now());
        proTransferService.save(proTransfer);
        copWarehouseService.warehouseTransfer(proTransfer);
        return ResultUtil.Success();
    }

}
